package com.examples.ry.graphics;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.ImageLoader;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;

public class ExperimentWithImages {
	public static final String resourceName = "yourFile.gif";

	public static void main(String[] args) {
		boolean useGIFBackground = false;
		final ImageData data = new ImageData(
				TransparentImages.class.getResourceAsStream(resourceName));
		final Display display = new Display();
		final Shell shell = new Shell(display, SWT.SHELL_TRIM);
		ImageLoader loader = new ImageLoader();
		ImageData[] imageDataArray;
		int imageDataIndex = 0;	
		Color shellBackground=shell.getBackground();
		imageDataArray = loader.load(resourceName);
		ImageData imageData = imageDataArray[0];
		Image image = new Image(display, imageData);
		int repeatCount = loader.repeatCount;
		
		
		Image offScreenImage = new Image(display, loader.logicalScreenWidth, loader.logicalScreenHeight);
		GC offScreenImageGC = new GC(offScreenImage);
		offScreenImageGC.setBackground(shellBackground);
		offScreenImageGC.fillRectangle(0, 0, loader.logicalScreenWidth, loader.logicalScreenHeight);
		
		while (loader.repeatCount == 0 || repeatCount > 0) {
			switch (imageData.disposalMethod) {
			case SWT.DM_FILL_BACKGROUND:
				/* Fill with the background color before drawing. */
				Color bgColor = null;
				if (useGIFBackground && loader.backgroundPixel != -1) {
					bgColor = new Color(display, imageData.palette.getRGB(loader.backgroundPixel));
				}
				offScreenImageGC.setBackground(bgColor != null ? bgColor : shellBackground);
				offScreenImageGC.fillRectangle(imageData.x, imageData.y, imageData.width, imageData.height);
				if (bgColor != null) bgColor.dispose();
				break;
			case SWT.DM_FILL_PREVIOUS:
				/* Restore the previous image before drawing. */
				offScreenImageGC.drawImage(
					image,
					0,
					0,
					imageData.width,
					imageData.height,
					imageData.x,
					imageData.y,
					imageData.width,
					imageData.height);
				break;
			}
								
			imageDataIndex = (imageDataIndex + 1) % imageDataArray.length;
			imageData = imageDataArray[imageDataIndex];
			image.dispose();
			image = new Image(display, imageData);
			offScreenImageGC.drawImage(
				image,
				0,
				0,
				imageData.width,
				imageData.height,
				imageData.x,
				imageData.y,
				imageData.width,
				imageData.height);
			
			/* Draw the off-screen image to the shell. */
			//shellGC.drawImage(offScreenImage, 0, 0);
			
			/* Sleep for the specified delay time (adding commonly-used slow-down fudge factors). */
			try {
				int ms = imageData.delayTime * 10;
				if (ms < 20) ms += 30;
				if (ms < 30) ms += 10;
				Thread.sleep(ms);
			} catch (InterruptedException e) {
			}
			
			/* If we have just drawn the last image, decrement the repeat count and start again. */
			if (imageDataIndex == imageDataArray.length - 1) repeatCount--;
		}
		
		
		
		
		
		

		// Image 1
		final Image img_0 = new Image(display, imageData);

		final Shell shell1 = new Shell(display, SWT.SHELL_TRIM);
		shell.addListener(SWT.Paint, new Listener() {
			public void handleEvent(Event event) {
				event.gc.drawImage(img_0, 10, 10);

			}
		});
		shell.setText("Load Transparent Images");
		shell.setSize(350, 180);
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		img_0.dispose();

		display.dispose();
	}
}
